package fr.inserm.um915.bdibd.model;

public class PairOfIndividual
	{
	//don't modify the order
	public enum Concordance
		{
		/** status =0 */
		UNAFFECTED,
		/** status == 1 */
		DISCORDANT,//one affected, one non-affected
		/** status == 2 */
		AFFECTED,
		/** unknown */
		NIL
		;
		public int toInt()
			{
			return ordinal();
			}
		}
	
	
	private Individual i1;
	private Individual i2;
	public PairOfIndividual(Individual i1,Individual i2)
		{
		if(i1.id< i2.id)
			{
			this.i1=i1;
			this.i2=i2;
			}
		else
			{
			this.i2=i1;
			this.i1=i2;
			}
		}
	
	public Individual getFirst() {
		return i1;
	}
	
	public Individual getSecond()
		{
		return i2;
		}
	
	public Individual get(int i)
		{
		return i==0?i1:i2;
		}
	
	@Override
	public int hashCode() {
		final int prime = 31;
		int result = 1;
		result = prime * result + (i1.hashCode());
		result = prime * result + (i2.hashCode());
		return result;
	}
	@Override
	public boolean equals(Object obj)
		{
		if (this == obj)
			return true;
		if (obj == null)
			return false;
		if (getClass() != obj.getClass())
			return false;
		PairOfIndividual other = (PairOfIndividual) obj;
		return i1.equals(other.i1)&& i2.equals(other.i2);
		}
	
	public String getLabel()
		{
		StringBuilder b=new StringBuilder();
		b.append(get(0).getFamily());
		b.append("|");
		b.append(get(0).getName());
		if(!get(0).getFamily().equalsIgnoreCase(get(1).getFamily()))
			{
			b.append("|");
			b.append(get(1).getFamily());
			}
		b.append("|");
		b.append(get(1).getName());
		return b.toString();
		}
	
	@Override
	public String toString() {
		return "("+i1+","+i2+")";
		}
	
	public boolean isSelfSelf()
		{
		return getFirst().equals(getSecond());
		}
	
	public boolean isParentChild()
		{
		if(!getFirst().getFamily().equals(getSecond().getFamily()))
			{
			return false;
			}
		for(int i=0;i< 2;++i)
			{
			Individual i1=(i==0?getFirst():getSecond());
			Individual i2=(i==0?getSecond():getFirst());
			
			if(i1.hasFather() &&
			   i1.getFatherId().equals(i2.getName())	
			   ) return true;
			if(i1.hasMother() &&
			   i1.getMotherId().equals(i2.getName())	
			   ) return true;
			
			}
		return false;
		}

	
	
	public Concordance getConcordance()
		{
		if(getFirst().getStatus()==2 && getSecond().getStatus()==2)
			{
			return Concordance.AFFECTED;
			}
		else if(getFirst().getStatus()==1 && getSecond().getStatus()==1)
			{
			return Concordance.UNAFFECTED;
			}
		else  if(
				(getFirst().getStatus()==2 && getSecond().getStatus()==1) ||
				(getFirst().getStatus()==1 && getSecond().getStatus()==2)
				)
			{
			return Concordance.DISCORDANT;
			}
		else if(getFirst().getStatus()<0 || getSecond().getStatus()<0)
			{
			return Concordance.NIL;
			}
		else
			{
			throw new IllegalArgumentException(
				"bad concordance:"+
					" first  status is "+getFirst().getStatus()+
					" second status is "+getSecond().getStatus()+
					" for pair("+i1+","+i2+")"
				);
			}
		}
	}
